package global

import (
	"github.com/spf13/viper"
	"miao-wails/backed/structs/base"
	"os"
	"path/filepath"
)

var (
	Conf = new(base.AppConf)
)

func InitConfig() (err error) {
	workDir := os.Getenv("miao_wails")
	if workDir == "" {
		workDir = "./"
	}
	v := viper.New()
	v.SetConfigType("yaml")
	v.SetConfigName("config")
	v.AddConfigPath(filepath.Dir(workDir))
	err = v.ReadInConfig()
	if err != nil {
		return
	}
	err = v.Unmarshal(Conf)
	if err != nil {
		return
	}
	if Conf.DataDir == "" {
		Conf.DataDir = base.DataDir
	}
	if Conf.Log.Name == "" {
		Conf.Log.Name = "miao.log"
		Conf.Log.Lv = "debug"
	}
	if Conf.DB.Supper == "" {
		Conf.DB.Supper = base.DBSupper
		Conf.DB.Source = base.DBSource
		Conf.DB.ShowSqlLog = base.DBShowSqlLog
	}
	if Conf.HttpPort == 0 {
		Conf.HttpPort = base.HttpPort
	}
	return err
}
